package com.vinxin.app.service

import cn.hutool.core.bean.BeanUtil
import com.alibaba.fastjson2.JSONPath
import com.vinxin.app.boss.feign.BossFeign
import com.vinxin.app.boss.model.QrcodeRequest
import com.vinxin.app.exception.ResponseCodeEnum
import com.vinxin.app.exception.VXException
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.http.HttpHeaders
import org.springframework.stereotype.Service

@Service
class UserService {

    @Autowired
    private lateinit var bossFeign: BossFeign

    fun getQrcode(): ByteArray {
        val result = bossFeign.getCaptchaRandKey()
        val qrId = JSONPath.extract(result, "$.qrId")?.toString() ?: ""

        val headers = HttpHeaders()
        headers.add("Accept", "image/avif,image/webp,image/apng,image/svg+xml,image/*,*/*;q: 0.8")
        headers.add("Accept-Encoding", "gzip, deflate, br, zstd")

        if (qrId.isNotBlank()) {
            return bossFeign.getQrcode(
                headers,
                BeanUtil.beanToMap(
                    QrcodeRequest(
                        content = qrId
                    )
                )
            ).toByteArray()
        } else {
            throw VXException(ResponseCodeEnum.BAD_REQUEST, "获取二维码失败")
        }
    }
}